package com.qys.livesMall.shop.service.impl;

import com.qys.livesMall.app.user.model.entity.H2AppUser;
import com.qys.livesMall.app.user.service.IH2AppUserService;
import com.qys.livesMall.common.constants.Constants;
import com.qys.livesMall.common.utils.bean.BeanProUtils;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.QysPage;
import com.qys.livesMall.kafka.dao.shop.ShopInterestInfoDao;
import com.qys.livesMall.kafka.dao.shop.ShopQueryHandle;
import com.qys.livesMall.kafka.entity.shop.ShopBrowseRecord;
import com.qys.livesMall.kafka.entity.shop.ShopGoodsInterestInfo;
import com.qys.livesMall.kafka.entity.shop.ShopInterestInfo;
import com.qys.livesMall.kafka.service.InterestHandleServer;
import com.qys.livesMall.shop.dao.H2ShopBaseDao;
import com.qys.livesMall.shop.modal.dto.shop.record.ShopRecordSearch;
import com.qys.livesMall.shop.modal.entity.H2ShopBase;
import com.qys.livesMall.shop.modal.entity.ShopGoodsInfo;
import com.qys.livesMall.shop.modal.vo.ShopBrowseRecordVO;
import com.qys.livesMall.shop.modal.vo.ShopGoodsInterestVO;
import com.qys.livesMall.shop.modal.vo.ShopUserInterestVO;
import com.qys.livesMall.shop.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @menu ShopAsyncServiceImpl
 * @date 2023/05/30 11:18
 */
@Service
public class ShopRecordServiceImpl implements ShopRecordService {

    @Autowired
    private IH2AppUserService appUserService;

    @Autowired
    private ShopQueryHandle shopQueryHandle;

    @Autowired
    private InterestHandleServer interestHandleServer;

    @Autowired
    private H2ShopBaseDao h2ShopBaseDao;

    @Autowired
    private ShopGoodsInfoService shopGoodsInfoService;



    @Override
    public QysPage<ShopBrowseRecordVO> searchShopBrowse(ShopRecordSearch search) {
        List<ShopBrowseRecord> shopBrowseRecords = shopQueryHandle.shopBrowseRecordPageByShopId(search.getShopId(), search.getPageNum(), search.getPageSize());
        QysPage<ShopBrowseRecordVO> res=QysPage.build(search);
        if (CollectionUtils.isEmpty(shopBrowseRecords)){
            return res;
        }
        List<Long> userIds=shopBrowseRecords.stream().map(ShopBrowseRecord::getUserId).collect(Collectors.toList());
        List<H2AppUser> users=appUserService.listByIds(userIds);
        Map<String,H2AppUser> userMap=users.stream().collect(Collectors.toMap(H2AppUser::getId, v->v,(v1, v2)->v1));
        List<ShopBrowseRecordVO> vos= BeanProUtils.copyListPropertiesV2(shopBrowseRecords, ShopBrowseRecordVO.class);
        vos.forEach(vo->{
            if (userMap.get(String.valueOf(vo.getUserId()))!=null)
            BeanUtils.copyProperties(userMap.get(String.valueOf(vo.getUserId())),vo);
        });
        res.setRecords(vos);
        return res;
    }

    @Override
    public void interest(ShopInterestInfo interestInfo) {
        int num=interestHandleServer.interest(interestInfo);
        if (num>0){
            H2ShopBase shopBase=h2ShopBaseDao.getById(interestInfo.getShopId());
            shopBase.setInterestNum(shopBase.getInterestNum()!=null?shopBase.getInterestNum()+1:1);
            h2ShopBaseDao.updateById(shopBase);
        }
    }

    @Override
    public void interestCancel(Long shopId,Long userId) {
        int num=interestHandleServer.interestCancel(shopId,userId);
        if (num > 0) {
            H2ShopBase shopBase=h2ShopBaseDao.getById(shopId);
            shopBase.setInterestNum(shopBase.getInterestNum()!=null&shopBase.getInterestNum()>0?shopBase.getInterestNum()-1:0);
            h2ShopBaseDao.updateById(shopBase);
        }
    }

    @Override
    public QysPage<ShopUserInterestVO> searchUserInterestShop(ShopRecordSearch search) {
        List<ShopInterestInfo> shopInterestInfos = shopQueryHandle.shopInterestInfoPageByUserId(SecurityUtils.getUserId(), search.getPageNum(), search.getPageSize());
        QysPage<ShopUserInterestVO> res=QysPage.build(search);
        if (CollectionUtils.isEmpty(shopInterestInfos)){
            return res;
        }
        List<Long> shopIds=shopInterestInfos.stream().map(ShopInterestInfo::getShopId).collect(Collectors.toList());
        List<H2ShopBase> shopBases=h2ShopBaseDao.listByIds(shopIds);
        Map<Long,H2ShopBase> shopBaseMap=shopBases.stream().collect(Collectors.toMap(H2ShopBase::getId, v->v,(v1, v2)->v1));
        List<ShopUserInterestVO> vos= BeanProUtils.copyListPropertiesV2(shopInterestInfos, ShopUserInterestVO.class);
        vos.forEach(vo-> {
            if (shopBaseMap.get(vo.getShopId())!=null) {
                BeanUtils.copyProperties(shopBaseMap.get(vo.getShopId()), vo);
            }
        });
        res.setRecords(vos);
        return res;
    }

    @Override
    public QysPage<ShopGoodsInterestVO> searchUserInterestShopGoods(ShopRecordSearch search) {
        List<ShopGoodsInterestInfo> shopGoodsInterestInfos = shopQueryHandle.shopGoodsInterestInfoPageByUserId(SecurityUtils.getUserId(), search.getPageNum(), search.getPageSize());
        QysPage<ShopGoodsInterestVO> res=QysPage.build(search);
        if (CollectionUtils.isEmpty(shopGoodsInterestInfos)){
            return res;
        }
        List<Long> shopGoodsIds=shopGoodsInterestInfos.stream().map(ShopGoodsInterestInfo::getShopGoodsId).collect(Collectors.toList());
        List<ShopGoodsInfo> shopGoodsInfos=shopGoodsInfoService.listByIds(shopGoodsIds);
        Map<Long,ShopGoodsInfo> goodsInfoMap=shopGoodsInfos.stream().collect(Collectors.toMap(ShopGoodsInfo::getId, v->v,(v1, v2)->v1));
        List<ShopGoodsInterestVO> vos= BeanProUtils.copyListPropertiesV2(shopGoodsInterestInfos, ShopGoodsInterestVO.class);
        vos.forEach(vo-> {
            if (goodsInfoMap.get(vo.getShopGoodsId())!=null) {
                ShopGoodsInfo info = goodsInfoMap.get(vo.getShopGoodsId());
                vo.setGoodsName(info.getGoodsName());
                vo.setGoodsImg(info.getImage());
                vo.setInterestNum(info.getInterestNum());
                vo.setOriginalPrice(info.getOriginalPrice());
                vo.setPrice(info.getPrice());
                vo.setStatus(info.isStatus()? Constants.YesNoInt.YES: Constants.YesNoInt.NO);
            }
        });
        res.setRecords(vos);
        return res;
    }

    @Override
    public int isInterestShop(Long shopId) {
        return interestHandleServer.isInterestShop(shopId,SecurityUtils.getUserId());

    }

    @Override
    public int isInterestShopGoods(Long shopGoodsId) {
        return interestHandleServer.isInterestShopGoods(shopGoodsId,SecurityUtils.getUserId());

    }

    @Override
    public void interestShopGoods(ShopGoodsInterestInfo interestInfo) {
        int num=interestHandleServer.interestShopGoods(interestInfo);
        if (num>0){
            ShopGoodsInfo info=shopGoodsInfoService.getById(interestInfo.getShopGoodsId());
            info.setInterestNum(info.getInterestNum()!=null?info.getInterestNum()+1:1);
            shopGoodsInfoService.updateById(info);
        }
    }

    @Override
    public void shopGoodsInterestCancel(Long shopGoodsId) {
        int num=interestHandleServer.shopGoodsInterestCancel(shopGoodsId,SecurityUtils.getUserId());
        if (num > 0) {
            ShopGoodsInfo info=shopGoodsInfoService.getById(shopGoodsId);
            if (info==null){
                return;
            }
            info.setInterestNum(info.getInterestNum()!=null?info.getInterestNum()-1:0);
            shopGoodsInfoService.updateById(info);
        }
    }


}

